package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.admin.model.User;
import com.dongdongshop.common.Result;
import com.dongdongshop.dto.SellerDTO;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.em.TokenEnum;
import com.dongdongshop.exception.LoginException;
import com.dongdongshop.service.UserService;
import com.dongdongshop.shiro.LoginToken;
import com.dongdongshop.util.JwtUtil;
import com.dongdongshop.util.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("login")
@Slf4j
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("login")
    public Result login(@RequestParam String username, @RequestParam String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装usernamepasswordToken
        LoginToken usernamePasswordToken = new LoginToken(username,password,TokenEnum.ADMIN);
        //调用subject
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e) {
            log.error("登录失败,原因{}",e);
            return Result.Response(ResultEnum.LOGIN_IS_NULL_ERROR);
        }catch (IncorrectCredentialsException e){
            log.error("登录失败,原因{}",e);
            return Result.Response(ResultEnum.LOGIN_IS_NULL_ERROR);
        }
        //登陆成功,封装jwt,将token返回给前段
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUid(user.getUid().toString());
        loginUser.setUname(user.getUname());
        String token = jwtUtil.createJwt(loginUser.getUid().toString(), JSONObject.toJSONString(loginUser), "loginUser");
        return Result.OK().setData(token);
    }

    @PostMapping("sellerLogin")
    public Result sellerLogin(@RequestParam String username, @RequestParam String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装usernamepasswordToken
        LoginToken usernamePasswordToken = new LoginToken(username,password,TokenEnum.SELLER);
        //调用subject
        try {
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException e) {
            log.error("登录失败,原因{}",e);
            return Result.Response(ResultEnum.LOGIN_IS_NULL_ERROR);
        }catch (IncorrectCredentialsException e){
            log.error("登录失败,原因{}",e);
            return Result.Response(ResultEnum.LOGIN_IS_NULL_ERROR);
        }catch (LoginException e) {
            log.error("登录失败,原因是{}",e.getMessage());
            return Result.Response(ResultEnum.ACCOUNT_STATUS_EXCEPTION);
        }
        //登陆成功,封装jwt,将token返回给前段
        SellerDTO user = (SellerDTO)SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUid(user.getSellerId());
        loginUser.setUname(user.getSellerId());
        String token = jwtUtil.createJwt(loginUser.getUid().toString(), JSONObject.toJSONString(loginUser), "loginUser");
        return Result.OK().setData(token);
    }


    @PostMapping
    public Result getAdminByName(@RequestParam String userName){
        User vo = userService.findUserByName(userName);
        return Result.OK().setData(vo);
    }
}
